package com.aduerr.v1shouyin.mvp.adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.entity.LoginAccountEntity;

import java.util.List;

/**
 * @author HI
 * @email dev1024@foxmail.com
 * @time 2019/5/9 14:51
 * @change
 * @chang time
 * @class describe
 */

public class LoginAccountAdapter extends RecyclerView.Adapter<LoginAccountAdapter.ViewHolder> {

    private Context context;
    private List<LoginAccountEntity> lists;
    private OnItemClickListener onItemClickListener;

    public LoginAccountAdapter(Context context) {
        this.context = context;
    }

    public LoginAccountAdapter(Context context, List<LoginAccountEntity> lists) {
        this.context = context;
        this.lists = lists;
    }

    public void updateData(List<LoginAccountEntity> lists) {
        this.lists = lists;
        notifyDataSetChanged();
    }

    public void addData(List<LoginAccountEntity> lists) {
        for(LoginAccountEntity lb : lists) {
            this.lists.add(lb);
        }
        notifyDataSetChanged();
    }

    public void delete(int position) {
        this.lists.remove(position);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_account
                , parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        LoginAccountEntity dataBean = lists.get(position);
        holder.tvAccount.setText(dataBean.getAccount());
        holder.ivDelete.setOnClickListener(v -> onItemClickListener.onDeleteClick(v,
                position));
        holder.itemView.setOnClickListener(v -> onItemClickListener.onItemClick(v,
                position));

    }

    @Override
    public int getItemCount() {
        return lists == null ? 0 : lists.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onDeleteClick(View view, int position);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvAccount;
        ImageView ivDelete;

        public ViewHolder(View itemView) {
            super(itemView);
            tvAccount = (TextView) itemView.findViewById(R.id.tv_account);
            ivDelete = (ImageView) itemView.findViewById(R.id.iv_delete);
        }
    }
}
